/**
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 19-10-23
 * Time: 下午8:34
 * To change this template use File | Settings | File Templates.
 */
(function($){

    var optionsAndDataMap={};

    var tempMap={};

    function renderDialog(i,divId){
        if (typeof($("#dataDetail"+divId)) != 'undefined') {
            $("#dataDetail"+divId).remove();
        }
        var row=optionsAndDataMap[divId].rows[i];

        var trs="";
        for(var j=0;j<optionsAndDataMap[divId].fields.length;j++){
            var field=optionsAndDataMap[divId].fields[j];
            //alert("field.name->"+field.name);
            var text=field.text;
            var value=row[field.name];
            //alert("value->"+value);
            if(typeof(field.inDetail)=="undefined"||field.inDetail==true) {
                trs += "<tr>" +
                    "   <td>" + text + "</td>";
                if (typeof(field.render) != 'undefined') {
                    var index=i;
                    // if(optionsAndDataMap[divId].offset>-1){
                    //     index+=optionsAndDataMap[divId].offset;
                    // }
                    value = field.render(row,index,parseInt(index) + optionsAndDataMap[divId].data.max * (optionsAndDataMap[divId].currentPage - 1));
                }
                if(value == null){
                    value = "";
                }
                trs += "   <td>" + value + "</td>";
                trs += "<tr>";
            }
        }


        var htmlStr=""+
            "<div class='modal fade bd-example-modal-lg' id='dataDetail"+divId+"' tabindex='-1' role='dialog' data-row='"+i+"' style='overflow:auto;overflow-y:scroll;'>                                                               "+
            "    <div class='modal-dialog modal-lg'>                                                                                                                 "+
            "        <div class='modal-content'>                                                                                                                     "+
            "            <div class='modal-header'>                                                                              "+
            "                <h4 class='modal-title'>"+optionsAndDataMap[divId].detailDialogTitle+"</h4>                                                                                                    "+
            "                <button type='button' class='close' data-dismiss='modal'>&times;</button>                                                                               "+
            "            </div>                                                                                                                                      "+
            "            <div class='modal-body'>                                                                                                                    "+
            "                <div class='dinging-detailed'>                                                                                                          "+
            "                    <table class='table table-hover table-bordered'>                                                                                                               "+
            trs;
        if(optionsAndDataMap[divId].operations.length>0){
            htmlStr+="<tr><td>操作</td>  " +
                "    <td name='operations'>";
            for(var o=0;o<optionsAndDataMap[divId].operations.length;o++){
                var operation=optionsAndDataMap[divId].operations[o];
                var opVisible=true;
                if(typeof(operation.visible) != 'undefined'){
                    opVisible=operation.visible(row);
                }
                if(opVisible){
                    htmlStr+="						<button type=\"button\" class=\"btn btn-link\" name='"+operation.name+"' data-row='"+i+"' operation-id='"+o+"'>"+operation.text+"</button>";
                }
            }
            htmlStr+="</td></tr>";
        }

        htmlStr+=
            "                    </table>                                                                                                                            "+
            "                </div>                                                                                                                                  "+
            "            </div>" +
            "<div class='modal-footer'>"+
            "<button type='button' class='btn btn-primary' data-dismiss='modal'>确定</button>"+
            "  </div>                                                                                                                                      "+
            "        </div>                                                                                                                                          "+
            "    </div>                                                                                                                                              "+
            "</div>                                                                                                                                                  ";

        //var dialog = $(htmlStr);
        $("body").append(htmlStr);
        //添加事件处理
        for(var o=0;o<optionsAndDataMap[divId].operations.length;o++){
            var operation=optionsAndDataMap[divId].operations[o];
            //alert(operation.name);
            $("#dataDetail"+divId+" button[name='"+operation.name+"']").click(function(){
                var rowId=$(this).attr("data-row");
                var operationId=$(this).attr("operation-id");
                //alert("rowId->"+rowId)
                var row=optionsAndDataMap[divId].rows[rowId];
                var preRow=null;
                if(parseInt(rowId)>0){
                    preRow=optionsAndDataMap[divId].rows[parseInt(rowId)-1];
                }
                var nextRow=null;
                if(parseInt(rowId)<optionsAndDataMap[divId].rows.length-1){
                    nextRow=optionsAndDataMap[divId].rows[parseInt(rowId)+1];
                }
                //alert(operationId);
                optionsAndDataMap[divId].operations[operationId].handler(row,preRow,nextRow,rowId);
            });
        }
        //optionsAndDataMap[divId].scrollTopTemp=document.body.scrollTop;
        //console.info("scrollTop"+scrollTopTemp);
        $("#dataDetail"+divId).modal('show');
        //$("#dataDetail"+divId).unbind("hide.bs.modal");
        // $("#dataDetail"+divId).on("hide.bs.modal",function(){
        //     document.body.scrollTop=optionsAndDataMap[divId].scrollTopTemp;
        // });

        if(typeof(optionsAndDataMap[divId].afterRenderDetail)!="undefined" ){
            optionsAndDataMap[divId].afterRenderDetail();
        }
    }

    function setColResizable(divId) {
        if($("#" + divId + "-table").length==0){//还没加载完
            setTimeout(function () {
                setColResizable(divId);
            },100);
        }else {
            $("#" + divId + "-table").colResizable({
                partialRefresh:true,
                liveDrag: false,
                gripInnerHtml: "<div class='grip'></div>",
                draggingClass: "dragging",
                onResize: function (e) {
                }
            });
            console.info("set colResizable")
        }
    }
    function render(divId){
        if (typeof($("#dataDetail"+divId)) != 'undefined') {
            $("#dataDetail"+divId).modal('hide');
        }
        if(optionsAndDataMap[divId].rows==null||optionsAndDataMap[divId].rows.length==0){
            var htmlStr="<div style=\"margin: 15px 0px; text-align: center;\">"+
                "    没有数据！"+
                "</div>";
            optionsAndDataMap[divId].dataDiv.html(htmlStr);
        }else{
            //alert(fields.length);
            var theadTh="";
            for(var i=0;i<optionsAndDataMap[divId].fields.length;i++){
                var field=optionsAndDataMap[divId].fields[i];
                if(field.inTable) {
                    if(field.wordWrap) {//自动换行
                        if (field.autoHidden) {
                            theadTh += "<th class='hidden-xs' style='text-align: center'>" + field.text + "</th>";
                        } else {
                            theadTh += "<th style='text-align: center'>" + field.text + "</th>";
                        }
                    }else{
                        if (field.autoHidden) {
                            theadTh += "<th class='hidden-xs' style='text-align: center;white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow: ellipsis;'>" + field.text + "</th>";
                        } else {
                            theadTh += "<th style='text-align: center;white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow: ellipsis;'>" + field.text + "</th>";
                        }
                    }
                }
            }
            if(optionsAndDataMap[divId].showDetail){
                theadTh += "<th style='text-align: center'>详情</th>";
            }
            var showOperationCount=0;
            for(var o=0;o<optionsAndDataMap[divId].operations.length;o++){
                var operation=optionsAndDataMap[divId].operations[o];
                var opVisible=true;
                if(typeof(operation.visible) != 'undefined'){
                    opVisible=operation.visible(row);
                }
                if(opVisible) {
                    showOperationCount++;
                }
            }
            if(showOperationCount>0) {
                if(optionsAndDataMap[divId].autoHideOperation) {
                    theadTh += "<th class='hidden-xs' style='text-align: center'>操作</th>";
                }else{
                    theadTh += "<th style='text-align: center'>操作</th>";
                }
            }
            //alert("theadTd"+theadTd);
            var dataTr="";
            for(var i=0;i<optionsAndDataMap[divId].rows.length;i++){
                var row=optionsAndDataMap[divId].rows[i];
                dataTr+="<tr data-id='"+i+"'>";
                for(var j=0;j<optionsAndDataMap[divId].fields.length;j++){
                    var field=optionsAndDataMap[divId].fields[j];
                    //alert("field.name->"+field.name);
                    if(field.inTable) {
                        var value = row[field.name];
                        //alert("value->"+value);
                        if (typeof (field.render) != 'undefined') {
                            var index = i;
                            if (optionsAndDataMap[divId].offset > -1) {
                                index += optionsAndDataMap[divId].offset;
                            }
                            value = field.render(row, index, index + optionsAndDataMap[divId].data.max * (optionsAndDataMap[divId].currentPage - 1));
                        }
                        if (value == null||value=="null") {
                            value = "";
                        }
                        if(field.wordWrap) {//自动换行
                            if (field.autoHidden) {
                                dataTr += "<td class='hidden-xs' style='text-align: center;vertical-align:middle;padding: 5px;'>" + value + "</td>";
                            } else {
                                dataTr += "<td style='text-align: center;vertical-align:middle;padding: 5px;'>" + value + "</td>";
                            }
                        }else{
                            var tip="";
                            if(value!=""){
                                tip="data-container=\"body\" data-toggle=\"popover\" data-placement=\"top\" data-content=\""+value+"\"";
                            }
                            if (field.autoHidden) {
                                dataTr += "<td "+tip+" class='hidden-xs' style='text-align: center;vertical-align:middle;padding: 5px;white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow: ellipsis;'>" + value + "</td>";
                            } else {
                                dataTr += "<td "+tip+" style='text-align: center;vertical-align:middle;padding: 5px;white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow: ellipsis;'>" + value + "</td>";
                            }
                        }
                    }
                }
                if(optionsAndDataMap[divId].showDetail){
                    dataTr+="<td class='' style='text-align: center;vertical-align:middle;padding: 5px;'>";
                    dataTr+="<button type=\"button\" class=\"btn btn-link\" name='detail' data-row='" + i + "'>查看</button>";
                    dataTr+="</td>";
                }
                if(showOperationCount>0){
                    if(optionsAndDataMap[divId].autoHideOperation){
                        dataTr+="<td name='operations' class='hidden-xs' style='text-align: center;vertical-align:middle;padding: 5px;'>";
                    }else{
                        dataTr+="<td name='operations' class='' style='text-align: center;vertical-align:middle;padding: 5px;'>";
                    }
                    for(var o=0;o<optionsAndDataMap[divId].operations.length;o++){
                        var operation=optionsAndDataMap[divId].operations[o];
                        var opVisible=true;
                        if(typeof(operation.visible) != 'undefined'){
                            opVisible=operation.visible(row);
                        }
                        if(opVisible) {
                            //dataTr += "						<a href='#' name='" + operation.name + "' data-row='" + i + "' operation-id='" + o + "'>" + operation.text + "</a>";
                            dataTr+="<button type=\"button\" class=\"btn btn-link\" name='" + operation.name + "' data-row='" + i + "' operation-id='" + o + "'>"+operation.text+"</button>";
                        }
                    }
                    dataTr+="				</td>";
                }
                dataTr+="    </tr>";
            }
            //alert("dataTr->"+dataTr);
            var tableHtmlStr="<table class='table table-bordered table-striped' id='"+divId+"-table'>"+
                "    <thead><tr>"+
                         theadTh+"" +
                     "</tr></thead>"+
                "    <tfoot><tr>"+
                        theadTh+"" +
                    "</tr></tfoot>"+
                "    <tbody>"+
                        dataTr+
                "    </tbody>"+
                "</table>";


            var pageHtmlStr="<div class='right-table-pagination'>"+
                "<nav>"+
                "   <ul class='pagination justify-content-center'> "+
                "           <li class='page-item'><button type='button' class='page-link' name='prev'><i class='glyphicon glyphicon-arrow-left'></i>上一页</button></li>&nbsp;"+
                "       <li class='page-item' style='line-height: 40px;'>"+
                "<select id='pagechanger"+divId+"'>" ;
            for(p=1;p<=optionsAndDataMap[divId].pages;p++){
                if(p==optionsAndDataMap[divId].currentPage){
                    pageHtmlStr+="<option value='"+p+"' selected>"+p+"</option>" ;
                }else{
                    pageHtmlStr+="<option value='"+p+"'>"+p+"</option>" ;
                }
            }
            pageHtmlStr+= "</select>"+
                "/"+optionsAndDataMap[divId].pages+"页<i class='d-none d-sm-inline'>，共"+optionsAndDataMap[divId].results+"条</i></li>"+
                "       &nbsp;<li class='page-item'><button type='button' class='page-link' name='next'>下一页<i class='glyphicon glyphicon-arrow-right'></i></button></li>"+
                "   </ul>"+
                "</nav>"+
                "</div>";
            //alert("tableHtmlStr->"+tableHtmlStr);
            var fieldShowSelect="";
            if(optionsAndDataMap[divId].showFieldSelect){
                fieldShowSelect="<div class=\"btn-group hidden-xs\" style=\"float: left;margin-top: 5px;margin-left: 5px;\">\n" +
                    "                    <button class=\"btn btn-primary btn-sm dropdown-toggle\" type=\"button\" data-toggle=\"dropdown\" aria-haspopup=\"true\" aria-expanded=\"false\">\n" +
                    "                        字段\n" +
                    "                    </button>\n" +
                    "                    <div class=\"dropdown-menu\" x-placement=\"bottom-start\" style=\"position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(0px, 31px, 0px);padding-top: 0px;padding-bottom: 15px;\">\n";
                for(var i=0;i<optionsAndDataMap[divId].fields.length;i++){
                    var field=optionsAndDataMap[divId].fields[i];
                    fieldShowSelect+="<div class=\"dropdown-item form-check\" style='height: 30px;' data-fn='"+field.name+"' name='fieldShowSelect'>\n";
                    if(field.inTable) {
                        fieldShowSelect+="<input type=\"checkbox\" class=\"form-check-input\" checked=\"\" style=\"height: 30px;width: 15px;\">\n";
                    }else{
                        fieldShowSelect+="<input type=\"checkbox\" class=\"form-check-input\" style=\"height: 30px;width: 15px;\">\n";
                    }
                    fieldShowSelect+="<label class=\"form-check-label\">"+field.text+"</label>\n" +
                        "</div>\n";
                }
                fieldShowSelect+="     </div>\n" +
                    "             </div>";
            }
            var pageSizeSelect="";
            if(optionsAndDataMap[divId].showPageSize) {
                pageSizeSelect = "<div style=\"float: right;padding-right:5px;text-align: right;margin-bottom: 0px;\">" +
                    "每页显示条数：<select name='dataGridPageSizeSelector'><option value='10'>10</option></select></div>";
            }
            //var htmlStr=pageSizeSelect+ts+tableHtmlStr+""+pageHtmlStr;
            var htmlStr="";
            if(optionsAndDataMap[divId].showFieldSelect||optionsAndDataMap[divId].showPageSize){
                htmlStr="<div style=\"border: 1px solid #ddd;border-bottom-width: 0px;line-height: 40px;height: 40px;\">";
                if(optionsAndDataMap[divId].showFieldSelect) {
                    htmlStr+=fieldShowSelect;
                }
                if(optionsAndDataMap[divId].showPageSize) {
                    htmlStr+=pageSizeSelect;
                }
                htmlStr+="</div>"+tableHtmlStr;
            }else{
                htmlStr=tableHtmlStr;
            }
            if(optionsAndDataMap[divId].showPages){
                htmlStr+=""+pageHtmlStr;
            }
            //optionsAndDataMap[divId].dataDiv.html("");
            optionsAndDataMap[divId].dataDiv.html(htmlStr);

            $('[data-toggle="popover"]').popover({
                html:true,
                trigger:"hover"
            })
            if(optionsAndDataMap[divId].colResizeEnable) {
                setColResizable(divId);
                console.info("colResizeEnable")
            }

            if(optionsAndDataMap[divId].showFieldSelect){
                $("div[name='fieldShowSelect']").click(function () {
                    var fieldName=$(this).attr("data-fn");
                    for(var i=0;i<optionsAndDataMap[divId].fields.length;i++){
                        var field=optionsAndDataMap[divId].fields[i];
                        if(field.name==fieldName) {
                            field.inTable=!field.inTable;
                            render(divId);
                            return;
                        }
                    }
                });
            }
            if(optionsAndDataMap[divId].showPageSize){
                //添加数据和绑定事件
                var str="";
                for(var i=1;i<11;i++){
                    if(optionsAndDataMap[divId].data.max!=null&&optionsAndDataMap[divId].data.max==i*10){
                        str += "<option selected value='" + (i * 10) + "'>" + (i * 10) + "</option>";
                    }else {
                        str += "<option value='" + (i * 10) + "'>" + (i * 10) + "</option>";
                    }
                }
                if(optionsAndDataMap[divId].data.max!=null&&optionsAndDataMap[divId].data.max==10000000) {
                    str += "<option selected value='10000000'>全部</option>";
                }else{
                    str += "<option value='10000000'>全部</option>";
                }
                $("select[name='dataGridPageSizeSelector']").empty();
                $("select[name='dataGridPageSizeSelector']").append(str);


                $("select[name='dataGridPageSizeSelector']").change(function(){
                    var pageSize=$(this).val();
                    optionsAndDataMap[divId].data.max=pageSize;
                    rload(1,divId);
                });
            }

            //绑定点击事件
            $("#pagechanger"+divId).change(function(){
                var selectPage=$(this).val();
                tempMap={};
                rload(selectPage,divId);
            });
            $("#"+divId+ " button[name='prev']").click(function(){
                if(optionsAndDataMap[divId].currentPage>1){
                    tempMap={};
                    rload(optionsAndDataMap[divId].currentPage-1,divId);
                }
            });
            if(optionsAndDataMap[divId].currentPage<=1){
                $("#"+divId+ " button[name='prev']").parent().addClass("disabled");
            }
            $("#"+divId+ " button[name='next']").click(function(){
                if(optionsAndDataMap[divId].currentPage<optionsAndDataMap[divId].pages){
                    tempMap={};
                    rload(optionsAndDataMap[divId].currentPage+1,divId);
                }
            });
            if(optionsAndDataMap[divId].currentPage>=optionsAndDataMap[divId].pages){
                $("#"+divId+ " button[name='next']").parent().addClass("disabled");
            }

            //行详情
            $("#"+divId+ " button[name='detail']").click(function(){
                var rowId=$(this).attr("data-row");
                //alert("rowId->"+rowId)
                var row=optionsAndDataMap[divId].rows[rowId];
                renderDialog(rowId,divId);
            });

            //行操作
            if(optionsAndDataMap[divId].operations.length>0) {
                for (var o = 0; o < optionsAndDataMap[divId].operations.length; o++) {
                    var operation = optionsAndDataMap[divId].operations[o];
                    //alert(operation.name);
                    $("#" + divId + " button[name='" + operation.name + "']").click(function () {
                        var rowId = $(this).attr("data-row");
                        var operationId = $(this).attr("operation-id");
                        //alert("rowId->"+rowId)
                        var row = optionsAndDataMap[divId].rows[rowId];
                        var preRow = null;
                        if (parseInt(rowId) > 0) {
                            preRow = optionsAndDataMap[divId].rows[parseInt(rowId) - 1];
                        }
                        var nextRow = null;
                        if (parseInt(rowId) < optionsAndDataMap[divId].rows.length - 1) {
                            nextRow = optionsAndDataMap[divId].rows[parseInt(rowId) + 1];
                        }
                        //alert(operationId);
                        optionsAndDataMap[divId].operations[operationId].handler(row, preRow, nextRow);
                        if (optionsAndDataMap[divId].markClickRow != null) {
                            $(this).parent().parent().parent().find("tr").css("background-color", "");
                            $(this).parent().parent().css("background-color", optionsAndDataMap[divId].markClickRow);
                        }
                    });
                }
            }

            //行点击高亮显示
            if(optionsAndDataMap[divId].highlightSelected) {
                if(typeof(tempMap[divId]) !="undefined"&&typeof(tempMap[divId].selectedIndex) !="undefined"){
                    $("#" + divId + " tbody tr[data-id='"+tempMap[divId].selectedIndex+"']").css("background", "#a3fac1");
                }
                $("#" + divId + " tbody tr").mousedown(function (event) {
                    //alert("dd");
                    //event.stopPropagation();
                    $("#" + divId + " tbody tr").css("background", "");
                    $(this).css("background", "#a3fac1");
                    tempMap[divId]={selectedIndex:$(this).attr("data-id")};
                });
                $("#" + divId + " thead tr").mousedown(function (event) {
                    //alert("dd");
                    //event.stopPropagation();
                    $("#" + divId + " tbody tr").css("background", "");
                    tempMap={};
                });
            }

            //行添加右击菜单
            if(optionsAndDataMap[divId].contextMenus.length>0) {
                var contextMenuItems = {};
                for (var i = 0; i < optionsAndDataMap[divId].contextMenus.length; i++) {
                    var menu = optionsAndDataMap[divId].contextMenus[i];
                    contextMenuItems[menu.name] = {
                        name: menu.text, icon: menu.icon, callback: function (key, opt) {
                            console.info("key " + opt.key + ",  opt opt " + opt.selector);
                            console.info(this);
                            var rowId = $(this).attr("data-id");
                            //var operationId=$(this).attr("operation-id");
                            //alert("rowId->"+rowId)
                            var row = optionsAndDataMap[divId].rows[rowId];
                            var preRow = null;
                            if (parseInt(rowId) > 0) {
                                preRow = optionsAndDataMap[divId].rows[parseInt(rowId) - 1];
                            }
                            var nextRow = null;
                            if (parseInt(rowId) < optionsAndDataMap[divId].rows.length - 1) {
                                nextRow = optionsAndDataMap[divId].rows[parseInt(rowId) + 1];
                            }
                            //alert(operationId);
                            menu.handler(row, preRow, nextRow);
                        }
                    };
                }
                $.contextMenu("destroy", {selector: "#" + divId + " table>tbody>tr"});
                $.contextMenu('create', {
                    selector: "#" + divId + " table>tbody>tr",
                    // callback: function(key, options) {
                    //     alert(options.selector);
                    // },
                    items: contextMenuItems
                });
            }
        }
        if(typeof(optionsAndDataMap[divId].afterRenderEach)!="undefined" ){
            optionsAndDataMap[divId].afterRenderEach();
        }
    }

    function rload(page,divId){
        optionsAndDataMap[divId].data["page"]=page;
        if(typeof(optionsAndDataMap[divId].webContext)!='undefined'){
            $.doSubmit({
                type:optionsAndDataMap[divId].type,
                submitUrl:optionsAndDataMap[divId].url,
                data:optionsAndDataMap[divId].data,
                async:optionsAndDataMap[divId].async,
                traditional:optionsAndDataMap[divId].traditional,
                showwait:optionsAndDataMap[divId].showwait,
                callBack:function(response){
                    var redata=$.parseJSON(response);
                    optionsAndDataMap[divId].currentPage=redata.currentPage;
                    optionsAndDataMap[divId].pages=redata.pages;
                    optionsAndDataMap[divId].results=redata.results;
                    optionsAndDataMap[divId].rows=redata.rows;
                    optionsAndDataMap[divId].rowsInitial=new Array();
                    if(optionsAndDataMap[divId].rows!=null) {
                        for (var i = 0; i < optionsAndDataMap[divId].rows.length; i++) {
                            optionsAndDataMap[divId].rowsInitial.push($.extend(true, {}, optionsAndDataMap[divId].rows[i]));
                        }
                    }
                    optionsAndDataMap[divId].offset=redata.offset;
                    optionsAndDataMap[divId].max=redata.max;
                    if(typeof(optionsAndDataMap[divId].beforeRender) != 'undefined' ){
                        optionsAndDataMap[divId].beforeRender(redata);
                    }
                    render(divId);
                    if(typeof(optionsAndDataMap[divId].afterRender)!="undefined" ){
                        optionsAndDataMap[divId].afterRender(redata);
                    }
                    if(typeof(optionsAndDataMap[divId].afterLoad)!="undefined" ){
                        optionsAndDataMap[divId].afterLoad(redata);
                    }
                },
                webContext:optionsAndDataMap[divId].webContext
            });
        }else{
            $.ajax({
                type: optionsAndDataMap[divId].type,
                url: optionsAndDataMap[divId].url,
                data: optionsAndDataMap[divId].data,
                async:optionsAndDataMap[divId].async,
                traditional:optionsAndDataMap[divId].traditional,
                showwait:optionsAndDataMap[divId].showwait,
                success:function(response){
                    if(typeof(response)!="object"&&response.indexOf("\<!DOCTYPE html\>")>0){
                        window.location.reload();
                        //return;
                    }
                    //alert(response);
                    var redata=$.parseJSON(response);
                    optionsAndDataMap[divId].currentPage=redata.currentPage;
                    optionsAndDataMap[divId].pages=redata.pages;
                    optionsAndDataMap[divId].results=redata.results;
                    optionsAndDataMap[divId].rows=redata.rows;
                    optionsAndDataMap[divId].rowsInitial=new Array();
                    if(optionsAndDataMap[divId].rows!=null) {
                        for (var i = 0; i < optionsAndDataMap[divId].rows.length; i++) {
                            optionsAndDataMap[divId].rowsInitial.push($.extend(true, {}, optionsAndDataMap[divId].rows[i]));
                        }
                    }
                    optionsAndDataMap[divId].offset=redata.offset;
                    optionsAndDataMap[divId].max=redata.max;
                    if(typeof(optionsAndDataMap[divId].beforeRender) != 'undefined' ){
                        optionsAndDataMap[divId].beforeRender(redata);
                    }
                    render(divId);
                    if(typeof(optionsAndDataMap[divId].afterRender) != 'undefined' ){
                        optionsAndDataMap[divId].afterRender(redata);
                    }
                    if(typeof(optionsAndDataMap[divId].afterLoad)!="undefined" ){
                        optionsAndDataMap[divId].afterLoad(redata);
                    }
                },
                complete: function(e, xhr, settings){
                    //alert(e.status);
                    if(e.status === 200){

                    }else if(e.status === 304){

                    }else{

                    }
                }
            });
        }
    }
    function load(divId){
        tempMap={};
        rload(1,divId);
    }



    $.fn.datagrid=function(method,options){
        var divId;
        var dataDiv;
        this.each(function () {
            dataDiv=$(this);
            divId=dataDiv.attr("id");
        });
        if(typeof(divId)=="undefined" ){
            return;
        }

        if(method=="getRows"){
            return optionsAndDataMap[divId].rows;
        }
        if(method=="getRow"){
            var rowIndex=options.index;
            var row=null;
            if(typeof(optionsAndDataMap[divId].rows)!="undefined"&&typeof(rowIndex)!="undefined"){
                row=optionsAndDataMap[divId].rows[rowIndex];
            }
            return row;
        }
        if(method=="setRow"){
            var rowIndex=options.index;
            var row=options.row;
            if(typeof(optionsAndDataMap[divId].rows)!="undefined"&&typeof(rowIndex)!="undefined"){
                optionsAndDataMap[divId].rows[rowIndex]=row;
            }
            return;
        }
        if(method=="render"){
            render(divId);
            return;
        }
        if(method=="resetRows"){
            if(optionsAndDataMap[divId].rowsInitial!=null){
                optionsAndDataMap[divId].rows=new Array();
                //console.info("rows->"+rows);
                for(var i=0;i<optionsAndDataMap[divId].rowsInitial.length;i++){
                    optionsAndDataMap[divId].rows.push($.extend(true, {}, optionsAndDataMap[divId].rowsInitial[i]));
                }
                //console.info("rows->"+rows);
            }
            return;
        }



        if(method=="load") {
            var defaults = {
                showwait: true,
                //beforeRender:null,//在渲染数据之前执行，接收到回调时立马执行。
                dataDiv: dataDiv,
                //调用load方法需要传的参数
                //data:null,//查询参数包括分页参数{page:10,max:12}
                //url:null,//查询数据的地址
                //fields:null,//[{name:"id",autoHidden:true,text:"编号",inTable:true,render:function(){}},{name:"name",autoHidden:false,text:"名称",inTable:true}]
                //operations:null,//[{name:"remove",text:"删除",hadler:function(row){}},{name:"edit",text:"编辑",hadler:function(row){}}]
                detailDialogTitle: "详情",//对话框title
                showDetail: true,
                //webContext:null,//网站根目录
                async: true,
                //afterLoad:null,
                //loadSum:null,
                //traditional:null,//是否深度解析
                //renderBlock: false,
                //reportShowFields:null,
                //afterRenderDetail:null,
                type:"post",
                operations:[],
                contextMenus:[],

                //去后台查询后返回结果存到这里的
                currentPage: 0,
                //pages:null,
                //results:null,
                //rows:null,
                offset: -1,
                max: -1,
                //showFields:null,
                //scrollTopTemp: 0,
                showPages:true,
                //showContextMenuTip:true,
                //showContextMenu:true,
                //markClickRow:null,
                highlightSelected:true,
                showPageSize:true,
                colResizeEnable:false,
                autoHideOperation:true,
                showFieldSelect:true
            };
            optionsAndDataMap[divId] = $.extend(defaults, options);
            if(typeof(optionsAndDataMap[divId].data.max) == 'undefined'){
                optionsAndDataMap[divId].data.max=10;
            }
        }else if(method=="reload"){
            optionsAndDataMap[divId] = $.extend(optionsAndDataMap[divId], options);
        }
        return this.each(function () {
            dataDiv=$(this);
            if(method=="load"){
                load(divId);
            }else if(method=="reload"){
                rload(optionsAndDataMap[divId].currentPage,divId);
            }else if(method=="loadLastPage"){
                var m=10;
                if(typeof(optionsAndDataMap[divId].data.max) != 'undefined'){
                    m=optionsAndDataMap[divId].data.max;
                }
                if(optionsAndDataMap[divId].results%m==0) {
                    rload(optionsAndDataMap[divId].pages + 1,divId);
                }else{
                    rload(optionsAndDataMap[divId].pages,divId);
                }
            }
        });
    }

})(jQuery);
